--小bit库
local function check_int(n)
 -- checking not float
 if(n - math.floor(n) > 0) then
  error("trying to use bitwise operation on non-integer!")
 end
end

local function to_bits(n)
 check_int(n)
 if(n < 0) then
  -- negative
  return to_bits(bit.bnot(math.abs(n)) + 1)
 end
 -- to bits table
 local tbl = {}
 local cnt = 1
 while (n > 0) do
  local last = math.fmod(n,2)
  if(last == 1) then
   tbl[cnt] = 1
  else
   tbl[cnt] = 0
  end
  n = (n-last)/2
  cnt = cnt + 1
 end

 return tbl
end

local function tbl_to_number(tbl)
 local n = #tbl

 local rslt = 0
 local power = 1
 for i = 1, n do
  rslt = rslt + tbl[i]*power
  power = power*2
 end

 return rslt
end

local function expand(tbl_m, tbl_n)
 local big = {}
 local small = {}
 if(#tbl_m > #tbl_n) then
  big = tbl_m
  small = tbl_n
 else
  big = tbl_n
  small = tbl_m
 end
 -- expand small
 for i = #small + 1, #big do
  small[i] = 0
 end

end

local function bit_or(m, n)
 local tbl_m = to_bits(m)
 local tbl_n = to_bits(n)
 expand(tbl_m, tbl_n)

 local tbl = {}
 local rslt = math.max(#tbl_m, #tbl_n)
 for i = 1, rslt do
  if(tbl_m[i]== 0 and tbl_n[i] == 0) then
   tbl[i] = 0
  else
   tbl[i] = 1
  end
 end

 return tbl_to_number(tbl)
end

local function bit_and(m, n)
 local tbl_m = to_bits(m)
 local tbl_n = to_bits(n)
 expand(tbl_m, tbl_n)

 local tbl = {}
 local rslt = math.max(#tbl_m, #tbl_n)
 for i = 1, rslt do
  if(tbl_m[i]== 0 or tbl_n[i] == 0) then
   tbl[i] = 0
  else
   tbl[i] = 1
  end
 end

 return tbl_to_number(tbl)
end

local function bit_not(n)

 local tbl = to_bits(n)
 local size = math.max(#tbl, 32)
 for i = 1, size do
  if(tbl[i] == 1) then
   tbl[i] = 0
  else
   tbl[i] = 1
  end
 end
 return tbl_to_number(tbl)
end

local function bit_xor(m, n)
 local tbl_m = to_bits(m)
 local tbl_n = to_bits(n)
 expand(tbl_m, tbl_n)

 local tbl = {}
 local rslt = math.max(#tbl_m, #tbl_n)
 for i = 1, rslt do
  if(tbl_m[i] ~= tbl_n[i]) then
   tbl[i] = 1
  else
   tbl[i] = 0
  end
 end

 --table.foreach(tbl, print)

 return tbl_to_number(tbl)
end

function bit_rshift(n, bits)
 check_int(n)

 local high_bit = 0
 if(n < 0) then
  -- negative
  n = bit_not(math.abs(n)) + 1
  high_bit = 2147483648 -- 0x80000000
 end

 for i=1, bits do
  n = n/2
  n = bit_or(math.floor(n), high_bit)
 end
 return math.floor(n)
end

-- logic rightshift assures zero filling shift
function bit_logic_rshift(n, bits)
 check_int(n)
 if(n < 0) then
  -- negative
  n = bit_not(math.abs(n)) + 1
 end
 for i=1, bits do
  n = n/2
 end
 return math.floor(n)
end

local function bit_lshift(n, bits)
 check_int(n)

 if(n < 0) then
  -- negative
  n = bit_not(math.abs(n)) + 1
 end

 for i=1, bits do
  n = n*2
 end
 return bit_and(n, 4294967295) -- 0xFFFFFFFF
end

local function bit_xor2(m, n)
 local rhs = bit_or(bit_not(m), bit_not(n))
 local lhs = bit_or(m, n)
 local rslt = bit_and(lhs, rhs)
 return rslt
end



bit = {
 -- bit operations
 bnot = bit_not,
 band = bit_and,
 bor  = bit_or,
 bxor = bit_xor,
 brshift = bit_rshift,
 lshift = bit_lshift,
 bxor2 = bit_xor2,
 rshift = bit_logic_rshift,

 -- utility func
 tobits = to_bits,
 tonumb = tbl_to_number,
}

--unicode转utf-8
function unicode_to_utf8(convertStr)

    if type(convertStr)~="string" then
        return convertStr
    end

    --local bit = require("bit")
    local resultStr=""
    local i=1
    while true do

        local num1=string.byte(convertStr,i)
        local unicode

        if num1~=nil and string.sub(convertStr,i,i+1)=="\\u" then
            unicode=tonumber("0x"..string.sub(convertStr,i+2,i+5))
            i=i+6
        elseif num1~=nil then
            unicode=num1
            i=i+1
        else
            break
        end

        if unicode <= 0x007f then
            resultStr=resultStr..string.char(bit.band(unicode,0x7f))
        elseif unicode >= 0x0080 and unicode <= 0x07ff then
            resultStr=resultStr..string.char(bit.bor(0xc0,bit.band(bit.rshift(unicode,6),0x1f)))
            resultStr=resultStr..string.char(bit.bor(0x80,bit.band(unicode,0x3f)))
        elseif unicode >= 0x0800 and unicode <= 0xffff then
            resultStr=resultStr..string.char(bit.bor(0xe0,bit.band(bit.rshift(unicode,12),0x0f)))
            resultStr=resultStr..string.char(bit.bor(0x80,bit.band(bit.rshift(unicode,6),0x3f)))
            resultStr=resultStr..string.char(bit.bor(0x80,bit.band(unicode,0x3f)))
        end
    end
    resultStr=resultStr..'\0'
    return resultStr
end

--错误输出
function myerrorhandler(err)
	print("ERROR",err)
end
--音乐
function music(s)
	body = Http.get("http://yz.8848mv.com:8899/v1/search?keyWord=" .. s .. "&offset=1&limit=20")
	return body
end
function card(s)
	body = Http.get("http://yz.8848mv.com:8899/v1/getURl?rid=" .. s .. "&br=320k")
	return body
end
--音乐查询
function music_tables(s)
	start = xpcall(music,myerrorhandler,s)
	start = tostring(start)
	if start == "true" then
		song_singer = body:gmatch('"artist":"(.-)"')
		pic_url = body:gmatch('"pic":"(.-)"')
		song_name = body:gmatch('"name":"(.-)"')
		song_id = body:gmatch('"rid":(%d+)')
		for i in song_singer do
			table.insert(song_singers,i)
		end
		for i in song_name do
			table.insert(song_names,i)
		end
		for i in pic_url do
			table.insert(pic_urls,i)
		end
		for i in song_id do
			table.insert(song_ids,i)
		end
		rep = Msg()
		for i=1,10 do
			if song_singers[i] == nil then
				break
			end
			list = (i .. ":" .. song_singers[i] .. "-" .. song_names[i] .."\n")
			rep = rep + list
		end
		return (rep .. "回复：#1/2/...")
	else
		return ("ERROR")
	end
end
--音乐查看
function music_look(i)
	start = xpcall(card,myerrorhandler,song_ids[i])
	start = tostring(start)
	if start == "true" then
		url = body:match('"url":"(.-)"')
		local __format__ = [[{"app":"com.tencent.structmsg","config":{"autosize":true,"forward":true,"type":"normal"},"desc":"音乐","meta":{"music":{"desc":"%s","jumpUrl":"%s","musicUrl":"%s","preview":"%s","sourceMsgId":"0","source_icon":"","source_url":"","tag":"音乐","title":"%s"}},"prompt":"[分享]%s","ver":"0.0.0.1","view":"music"}]]
		__format__ = string.format(__format__, song_singers[i], url, url, pic_urls[i], song_names[i], song_names[i])
		return (__format__)
	else
		url = "https://other-web-ri01-sycdn.kuwo.cn/ec26fce2849582c411651cdf992af06b/6013fee7/resource/n1/5/67/2779156982.mp3"
		pic_url = "http://pic.51yuansu.com/pic2/cover/00/31/10/5810a32640ac7_610.jpg"
		local __format__ = [[{"app":"com.tencent.structmsg","config":{"autosize":true,"forward":true,"type":"normal"},"desc":"音乐","meta":{"music":{"desc":"%s","jumpUrl":"%s","musicUrl":"%s","preview":"%s","sourceMsgId":"0","source_icon":"","source_url":"","tag":"出错啦！！！","title":"%s"}},"prompt":"[分享]%s","ver":"0.0.0.1","view":"music"}]]
		__format__ = string.format(__format__, "错误", url, url, pic_url, "错误", "错误")
		return (__format__)
	end
end
--以图搜图
function picurl(s)
	body,isSuccessful,code,message = Http.get(s)
	return body
end
function find_pic(s)
	api_url = "https://hibiapi.getloli.com/api/sauce?url=" .. s
	start = xpcall(picurl,myerrorhandler,api_url)
	start = tostring(start)
	if start == "true" then
		if body:find(s) then
			return ("出错了！！")
		else
			json = body:match('results":%[(.-),{"header"')
			print(json)
			similarity = tostring(json:match('"similarity":"(%d+.%d+)"'))
			pic_url = json:match('"thumbnail":"(.-)"'):gsub("\\","")
			title = tostring(json:match('"title":"(.-)"'))
			pixiv_id = tostring(json:match('"pixiv_id":(%d+)'))
			member_name = tostring(json:match('"member_name":"(.-)"'))
			source = tostring(json:match('"ext_urls":%[(.-)%]')):gsub("\\","")
			if pixiv_id == "nil" then
				return pic_url, ('相似度：' .. similarity .. '\n来源：' .. source)
			else
				return  pic_url, ('相似度：' .. similarity .. '\n标题：' .. title .. '\n作者：' .. member_name .. '\n图片p站id：' .. pixiv_id)
			end
		end
	else
		pic_url = "http://pic.51yuansu.com/pic2/cover/00/31/10/5810a32640ac7_610.jpg"
		return pic_url, ("网络错误！！")
	end	
end

--table去重
function table_unique(s)
	local check = {};
	local n = {};
	for key , value in pairs(s) do
		if not check[value] then
			n[key] = value
			check[value] = value
		end
	end
	return n
end

function readdata()
	local f= io.open("test.txt","r")
	io.input(f)
	x = f:read("*a")
	return x
end

function writedata(i)
	local f= io.open("test.txt","a+")
	io.output(f)
	f:write(i)
	f:write("\n")
	f:close()
end